/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.site.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.site.entity.SiteArticle;
import com.jeesite.modules.site.service.SiteArticleService;

/**
 * 文章Controller
 * @author ben
 * @version 2018-05-02
 */
@Controller
@RequestMapping(value = "${adminPath}/site/siteArticle")
public class SiteArticleController extends BaseController {

	@Autowired
	private SiteArticleService siteArticleService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public SiteArticle get(String id, boolean isNewRecord) {
		return siteArticleService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("site:siteArticle:view")
	@RequestMapping(value = {"list", ""})
	public String list(SiteArticle siteArticle, Model model) {
		model.addAttribute("siteArticle", siteArticle);
		return "modules/site/siteArticleList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("site:siteArticle:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<SiteArticle> listData(SiteArticle siteArticle, HttpServletRequest request, HttpServletResponse response) {
		Page<SiteArticle> page = siteArticleService.findPage(new Page<SiteArticle>(request, response), siteArticle); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("site:siteArticle:view")
	@RequestMapping(value = "form")
	public String form(SiteArticle siteArticle, Model model) {
		model.addAttribute("siteArticle", siteArticle);
		return "modules/site/siteArticleForm";
	}

	/**
	 * 保存文章
	 */
	@RequiresPermissions("site:siteArticle:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated SiteArticle siteArticle) {
		siteArticleService.save(siteArticle);
		return renderResult(Global.TRUE, "保存文章成功！");
	}
	
	/**
	 * 停用文章
	 */
	@RequiresPermissions("site:siteArticle:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(SiteArticle siteArticle) {
		siteArticle.setStatus(SiteArticle.STATUS_DISABLE);
		siteArticleService.updateStatus(siteArticle);
		return renderResult(Global.TRUE, "停用文章成功");
	}
	/**
	 * 启用文章
	 */
	@RequiresPermissions("site:siteArticle:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(SiteArticle siteArticle) {
		siteArticle.setStatus(SiteArticle.STATUS_NORMAL);
		siteArticleService.updateStatus(siteArticle);
		return renderResult(Global.TRUE, "启用文章成功");
	}
	/**
	 * 删除文章
	 */
	@RequiresPermissions("site:siteArticle:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(SiteArticle siteArticle) {
		siteArticleService.delete(siteArticle);
		return renderResult(Global.TRUE, "删除文章成功！");
	}
	
}